package scm.inventoryControl.managedbean;

import crm.salesOrderProcessing.session.ProductManageSessionBean;
import scm.inventoryControl.entity.ProductBatchRecordEntity;
import scm.inventoryControl.entity.ProductInventoryLevelEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "createRecordManagedBean")
@ViewScoped
public class CreateRecordManagedBean implements Serializable {

    @EJB
    private ProductManageSessionBean productSessionBean;
    @EJB
    private InventoryControlSessionBean inventorySessionBean;
    
    private List<ProductBatchRecordEntity> productRecordList;
    private ProductBatchRecordEntity productBatchRecord;
    private String productName;
    private ProductInventoryLevelEntity productInventoryLevel;
     

    public CreateRecordManagedBean() {
        productBatchRecord = new ProductBatchRecordEntity();
        productRecordList = new ArrayList<ProductBatchRecordEntity>();
    }

    public List<String> getProductNames() {
        return productSessionBean.getProductNames();
    }

    public void deleteProductRecord(ProductBatchRecordEntity productBatchRecord) {
        getProductRecordList().remove(productBatchRecord);
    }

    public void addProductRecord(ActionEvent event) throws IOException {
        productInventoryLevel = inventorySessionBean.getInventoryByProductName(productName);
        productBatchRecord.setProductInventoryLevel(productInventoryLevel);
        getProductRecordList().add(productBatchRecord);
        productBatchRecord = new ProductBatchRecordEntity();
    }

    public void submitRecord(ActionEvent event) throws IOException {//save record &  update inventory level
        for (int i = 0; i < getProductRecordList().size(); i++) {
            inventorySessionBean.saveProductRecord(getProductRecordList().get(i));
             inventorySessionBean.adjustInventoryLevel(getProductRecordList().get(i));
        }
        //connected inventoryLevel to productRecords
       // inventorySessionBean.connectInventoryToRecords(getProductRecordList());
        productRecordList.clear();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Records has been successfully saved.", ""));
    }

   

    public List<ProductBatchRecordEntity> getProductRecordList() {
        return productRecordList;
    }

    public void setProductRecordList(List<ProductBatchRecordEntity> productRecordList) {
        this.productRecordList = productRecordList;
    }

    public ProductBatchRecordEntity getProductBatchRecord() {
        return productBatchRecord;
    }

    public void setProductBatchRecord(ProductBatchRecordEntity productBatchRecord) {
        this.productBatchRecord = productBatchRecord;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public ProductInventoryLevelEntity getProductInventoryLevel() {
        return productInventoryLevel;
    }

    public void setProductInventoryLevel(ProductInventoryLevelEntity productInventoryLevel) {
        this.productInventoryLevel = productInventoryLevel;
    }

 
}
